// 表情分析算法 - 基于面部特征点位置计算
export const analyzeExpression = (face) => {
  if (!face || !face.scaledMesh) return 'neutral';
  
  // 获取关键特征点坐标
  const getPoint = (index) => face.scaledMesh[index];
  
  // 眼睛相关点
  const leftEyeTop = getPoint(159); // 左眼上
  const leftEyeBottom = getPoint(145); // 左眼下
  const rightEyeTop = getPoint(386); // 右眼上
  const rightEyeBottom = getPoint(374); // 右眼下
  
  // 嘴巴相关点
  const mouthLeft = getPoint(61); // 嘴左
  const mouthRight = getPoint(291); // 嘴右
  const mouthTop = getPoint(0); // 嘴上
  const mouthBottom = getPoint(17); // 嘴下
  
  // 眉毛相关点
  const leftBrowTop = getPoint(285); // 左眉上
  const leftBrowBottom = getPoint(276); // 左眉下
  const rightBrowTop = getPoint(46); // 右眉上
  const rightBrowBottom = getPoint(55); // 右眉下
  
  // 计算垂直距离的辅助函数
  const verticalDistance = (top, bottom) => Math.abs(top[1] - bottom[1]);
  // 计算水平距离的辅助函数
  const horizontalDistance = (left, right) => Math.abs(left[0] - right[0]);
  
  // 计算各部分尺寸
  const leftEyeHeight = verticalDistance(leftEyeTop, leftEyeBottom);
  const rightEyeHeight = verticalDistance(rightEyeTop, rightEyeBottom);
  const eyeAvgHeight = (leftEyeHeight + rightEyeHeight) / 2;
  
  const mouthWidth = horizontalDistance(mouthLeft, mouthRight);
  const mouthHeight = verticalDistance(mouthTop, mouthBottom);
  
  const leftBrowHeight = verticalDistance(leftBrowTop, leftBrowBottom);
  const rightBrowHeight = verticalDistance(rightBrowTop, rightBrowBottom);
  const browAvgHeight = (leftBrowHeight + rightBrowHeight) / 2;
  
  // 计算相对比例（用于表情判断）
  const mouthOpenRatio = mouthHeight / mouthWidth;
  const eyeOpenRatio = eyeAvgHeight / (mouthWidth * 0.3); // 相对比例
  const browRaiseRatio = browAvgHeight / (eyeAvgHeight * 1.5);
  
  // 表情判断逻辑
  // 惊讶: 嘴巴张开较大，眼睛睁大，眉毛上扬
  if (mouthOpenRatio > 0.35 && eyeOpenRatio > 1.2 && browRaiseRatio > 1.1) {
    return 'surprised';
  }
  
  // 开心: 嘴角上扬（通过额外点判断），眼睛可能眯起
  const leftMouthCorner = getPoint(57);
  const rightMouthCorner = getPoint(287);
  const mouthCornerRatio = (rightMouthCorner[1] + leftMouthCorner[1]) / 2 - mouthTop[1];
  
  if (mouthCornerRatio < -3 && mouthOpenRatio > 0.15) {
    return 'happy';
  }
  
  // 生气: 眉毛下压，嘴巴紧闭或嘴角下垂
  if (browRaiseRatio < 0.7 && mouthOpenRatio < 0.15 && mouthCornerRatio > 2) {
    return 'angry';
  }
  
  // 悲伤: 嘴角下垂，眉毛可能上扬
  if (mouthCornerRatio > 3 && browRaiseRatio > 1.05) {
    return 'sad';
  }
  
  // 默认中性
  return 'neutral';
};
    